Authentication System
This document explains the Authentication System for the Agentic Browser extension. It focuses on:
The useAuth hook that manages user authentication state, token handling, and session management
The SignInScreen component that provides login interfaces for Google OAuth and a demo GitHub flow
The authentication flow from initial login through token refresh cycles
Browser storage integration and automatic logout mechanisms
Examples of authentication state management, error handling, and security considerations
Backend integration points and token validation processes
The authentication system spans the extension’s side panel (React + browser APIs) and the backend (FastAPI). The key files are:
Hook for authentication state and token lifecycle
Login UI component
Application shell that renders the login screen or settings based on auth state
Settings UI that displays tokens and exposes manual refresh/logout actions
Backend API surface for token exchange and refresh
Diagram sources
Section sources
useAuth hook
Initializes auth state from browser local storage
Detects token age and refreshes automatically when appropriate
Exposes login via browser.identity OAuth, GitHub demo login, logout, and manual refresh
Provides helpers to compute token age and expiry
SignInScreen component
Renders two login options: Google OAuth and GitHub demo
Uses styled buttons and animations for UX
App shell
Renders SignInScreen when unauthenticated or UnifiedSettingsMenu when authenticated
Handles first-time setup redirection flag
Settings UI
Displays token info and exposes manual refresh and logout actions
Section sources
The authentication flow integrates browser identity APIs, a backend service, and local storage. The diagram below maps the actual code paths.
routers/github.py" participant Store as "browser.storage.local" UI->>Hook : "handleLogin()" Hook->>Browser : "launchWebAuthFlow(authUrl)" Browser-->>Hook : "redirectResponse(code)" Hook->>Backend : "POST /exchange-code {code, redirect_uri}" Backend-->>Hook : "{access_token, refresh_token, expires_in}" Hook->>Store : "set googleUser (token, timestamps)" Store-->>Hook : "onChange(googleUser)" Hook-->>UI : "user state updated" Hook->>Hook : "checkAndRefreshToken()" Hook->>Backend : "POST /refresh-token {refresh_token}" Backend-->>Hook : "{access_token, expires_in}" Hook->>Store : "update googleUser"
Diagram sources
useAuth Hook#
The hook encapsulates:
Initialization from browser storage
Automatic token refresh based on token age
Manual refresh and logout
Token age/expiry computation
First-time setup flag handling
Key behaviors:
On mount, reads stored user data and triggers token refresh checks
Uses a threshold to decide whether to refresh the access token
Persists updated tokens back to storage and updates React state
Exposes handlers for Google OAuth login, GitHub demo login, logout, and manual refresh
Provides UI helpers to display token age and expiry
AND has refreshToken?"} NeedsRefresh --> |Yes| Refresh["refreshAccessToken(refreshToken)"] Refresh --> RefreshOK{"Refresh OK?"} RefreshOK --> |Yes| Save["Update googleUser in storage
and set user state"] RefreshOK --> |No| StatusFail["Set status: re-authenticate"] NeedsRefresh --> |No| Expired{"tokenAge > expires_in
AND no refreshToken?"} Expired --> |Yes| StatusExpire["Set status: expired"] Expired --> |No| StatusValid["Set status: valid"] Save --> DoneInit StatusFail --> DoneInit StatusExpire --> DoneInit StatusValid --> DoneInit
Diagram sources
Section sources
SignInScreen Component#
The component renders:
A hero section with animated visuals
Two login buttons:
Continue with Google (OAuth)
Continue with GitHub (demo bypass)
Responsive styling and hover effects
Diagram sources
Section sources
App Shell Integration#
The App component:
Consumes useAuth to determine whether to render SignInScreen or UnifiedSettingsMenu
Handles first-time setup redirection based on a storage flag
Subscribes to storage changes to stay in sync with the hook
Diagram sources
Section sources
Settings UI (Tokens and Actions)#
The settings UI surfaces:
Access token visibility toggle
Refresh token visibility toggle (blurred)
Manual refresh button (when refresh token present)
Logout button
Diagram sources
Section sources
Frontend-to-backend dependencies
useAuth.ts calls backend endpoints for token exchange and refresh
App.tsx depends on useAuth for rendering decisions
Settings components depend on useAuth for token display and actions
Backend routing
api/main.py registers routers under various prefixes
routers/github.py defines a GitHub endpoint used by services
services/github_service.py orchestrates GitHub-related operations
Diagram sources
Section sources
Token refresh threshold
The hook refreshes tokens before they reach a configured age threshold, reducing latency during requests
Local storage synchronization
Subscribing to storage changes ensures UI remains consistent across sessions
UI responsiveness
Loading states and alerts provide feedback during long-running operations like OAuth and network requests
[No sources needed since this section provides general guidance]
Common issues and resolutions:
Authentication cancelled or denied
The hook detects cancellation/denial keywords and shows a user-friendly alert
Backend service not running
Errors during token exchange or refresh trigger alerts instructing to verify backend availability
Token expired or refresh failed
The hook sets explicit status messages and advises re-authentication when refresh fails
Manual refresh unavailable
If no refresh token is present, the UI disables manual refresh and prompts re-login
Section sources
Token storage
Tokens are stored in browser local storage; consider encrypting sensitive fields for production
Token exposure
The settings UI supports toggling token visibility; use caution when sharing screens
Refresh token handling
Refresh tokens enable seamless renewal; ensure secure transport and storage
OAuth consent
The Google OAuth flow requests offline access and broad scopes; review and minimize scopes as needed
[No sources needed since this section provides general guidance]
The Authentication System combines a React hook, a browser-native OAuth flow, and a backend service to deliver a robust login experience. It supports automatic token refresh, manual refresh, logout, and persistent session state across browser restarts via local storage. The UI components provide clear feedback and controls for token management, while the backend routes integrate with services that consume authenticated tokens.